package com.example.administrator.myfragment;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;

/**
 * Created by wq on 2018/10/30.
 *
 * https://blog.csdn.net/zengchao2013/article/details/80105448
 *
 * Fragment setArguments时的java.lang.IllegalStateException: Fragment already active异常主要是使用setArguments()传递参数导致的：

 在 Fragment 没有被添加到 FragmentManager 之前，我们可以通过 Fragment.setArguments() 来设置参数，并在 Fragment 中，使用 getArguments() 来取得参数。在 Fragment 被添加到 FragmentManager 后，一旦被使用，我们再次调用 setArguments() 将会导致 java.lang.IllegalStateException: Fragment already active 异常。
 ---------------------
 作者：圣保罗
 来源：CSDN
 原文：https://blog.csdn.net/zengchao2013/article/details/80105448
 版权声明：本文为博主原创文章，转载请附上博文链接！
 */
public class MainActivity extends FragmentActivity implements View.OnClickListener , OneFragment.FragmentCallBack {
    private OneFragment mOneFragment;
    private TwoFragment mTwoFragment;
    private ThreeFragment mThreeFragment;
    private FourFragment mFourFragment;


    private LinearLayout ll_one;
    private LinearLayout ll_two;
    private LinearLayout ll_three;
    private LinearLayout ll_four;
    private ImageView iv_one ;
    private ImageView iv_two ;
    private ImageView iv_three ;
    private ImageView iv_four ;

    private TextView tv_three ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        // 第一次启动时选中第0个tab
        setTabSelection(0);

        iniData () ;
    }

    /**
     * 在这里获取到每个需要用到的控件的实例，并给它们设置好必要的点击事件。
     */

    private void initViews() {
        iv_one = (ImageView) findViewById(R.id.iv_one);
        iv_two = (ImageView) findViewById(R.id.iv_two);
        iv_three = (ImageView) findViewById(R.id.iv_three);
        iv_four = (ImageView) findViewById(R.id.iv_four);

        tv_three = (TextView) findViewById(R.id.tv_three);
        ll_one = (LinearLayout) findViewById(R.id.ll_one);
        ll_two = (LinearLayout) findViewById(R.id.ll_two);
        ll_three = (LinearLayout) findViewById(R.id.ll_three);
        ll_four = (LinearLayout) findViewById(R.id.ll_four);
        ll_one.setOnClickListener(this);
        ll_two.setOnClickListener(this);
        ll_three.setOnClickListener(this);
        ll_four.setOnClickListener(this);
    }

    private void iniData () {

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_one:
                // 当点击了消息tab时，选中第1个tab
                setTabSelection(0);
                break;
            case R.id.ll_two:
                // 当点击了联系人tab时，选中第2个tab
                setTabSelection(1);
                break;
            case R.id.ll_three:
                // 当点击了动态tab时，选中第3个tab
                setTabSelection(2);
                break;
            case R.id.ll_four:
                // 当点击了设置tab时，选中第4个tab
                setTabSelection(3);
                break;
            default:
                break;
        }

    }

    /**
     * 根据传入的index参数来设置选中的tab页。
     *
     * @param index
     * 每个tab页对应的下标。0表示消息，1表示联系人，2表示动态，3表示设置。
     */
    private void setTabSelection(int index) {
        // 每次选中之前先清楚掉上次的选中状态
        clearSelection();
        // 开启一个Fragment事务
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);
        switch (index) {
            case 0:
                iv_one.setBackgroundResource(R.drawable.icon_maintab_home_press);

                if (mOneFragment == null) {
                    // 如果mOneFragment为空，则创建一个并添加到界面上
                    mOneFragment = new OneFragment();
                    Bundle bundle = new Bundle();
                    bundle.putString("message", index+"");
                    mOneFragment.setArguments(bundle);
                    transaction.add(R.id.content, mOneFragment);
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString("message", index+"");
//                    mOneFragment.setArguments(bundle);
                    mOneFragment.getArguments().clear();
                    mOneFragment.getArguments().putAll(bundle);
                    transaction.show(mOneFragment);
                }
//                Bundle bundle = new Bundle();
//                bundle.putString("message", index+"");
//                mOneFragment.setArguments(bundle);
                break;
            case 1:
                // 当点击了联系人tab时，改变控件的图片和文字颜色
                iv_two.setBackgroundResource(R.drawable.icon_maintab_zichan_press);
                if (mTwoFragment == null) {
                    // 如果mTwoFragment为空，则创建一个并添加到界面上
                    mTwoFragment = new TwoFragment();
                    Bundle bundle2 = new Bundle();
                    bundle2.putString("message", index+"");
                    mTwoFragment.setArguments(bundle2);
                    transaction.add(R.id.content, mTwoFragment);
                } else {
                    Bundle bundle2 = new Bundle();
                    bundle2.putString("message", index+"");
//                    mTwoFragment.setArguments(bundle2);
                    mTwoFragment.getArguments().clear();
                    mTwoFragment.getArguments().putAll(bundle2);
                    transaction.show(mTwoFragment);
                }
//                Bundle bundle2 = new Bundle();
//                bundle2.putString("message", index+"");
//                mTwoFragment.setArguments(bundle2);
                break;
            case 2:
                // 当点击了动态tab时，改变控件的图片和文字颜色
                iv_three.setBackgroundResource(R.drawable.icon_maintab_person_press);




                if (mThreeFragment == null) {
                    // 如果mThreeFragment为空，则创建一个并添加到界面上
                    mThreeFragment = new ThreeFragment();
                    Bundle bundle3 = new Bundle();
                    bundle3.putString("message", index+"");
                    mThreeFragment.setArguments(bundle3);
                    transaction.add(R.id.content, mThreeFragment);
                } else {
                    // 如果mThreeFragment不为空，则直接将它显示出来
                    Bundle bundle3 = new Bundle();
                    bundle3.putString("message", index+"");
//                    mThreeFragment.setArguments(bundle3);
                    mThreeFragment.getArguments().clear();
                    mThreeFragment.getArguments().putAll(bundle3);

                    transaction.show(mThreeFragment);

                }
//                Bundle bundle3 = new Bundle();
//                bundle3.putString("message", index+"");
//                mThreeFragment.setArguments(bundle3);
                break;
            case 3:
            default:
                // 当点击了设置tab时，改变控件的图片和文字颜色
                iv_four.setBackgroundResource(R.drawable.icon_maintab_person_press);



                if (mFourFragment == null) {
                    // 如果mFourFragment为空，则创建一个并添加到界面上
                    mFourFragment = new FourFragment();
                    Bundle bundle4 = new Bundle();
                    bundle4.putString("message", index+"");
                    mFourFragment.setArguments(bundle4);

                    transaction.add(R.id.content, mFourFragment);
                } else {
                    // 如果mFourFragment不为空，则直接将它显示出来
                    Bundle bundle4 = new Bundle();
                    bundle4.putString("message", index+"");
//                    mFourFragment.setArguments(bundle4);
                    mFourFragment.getArguments().clear();
                    mFourFragment.getArguments().putAll(bundle4);
                    transaction.show(mFourFragment);
                }
//                Bundle bundle4 = new Bundle();
//                bundle4.putString("message", index+"");
//                mFourFragment.setArguments(bundle4);
                break;
        }
        transaction.commit();
    }

    /**
     * 将所有的Fragment都置为隐藏状态。
     *
     * @param transaction
     * 用于对Fragment执行操作的事务
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (mOneFragment != null) {
            transaction.hide(mOneFragment);
        }
        if (mTwoFragment != null) {
            transaction.hide(mTwoFragment);
        }
        if (mThreeFragment != null) {
            transaction.hide(mThreeFragment);
        }
        if (mFourFragment != null) {
            transaction.hide(mFourFragment);
        }
    }

    /**
     * 清除掉所有的选中状态。
     */
    private void clearSelection() {
//        ll_one.setBackgroundColor(0xffffffff);
//        ll_two.setBackgroundColor(0xffffffff);
//        ll_three.setBackgroundColor(0xffffffff);
//        ll_four.setBackgroundColor(0xffffffff);

        iv_one.setBackgroundResource(R.drawable.icon_maintab_home_normal);
        iv_two.setBackgroundResource(R.drawable.icon_maintab_zichan_normal);
        iv_three.setBackgroundResource(R.drawable.icon_maintab_person_normal);
        iv_four.setBackgroundResource(R.drawable.icon_maintab_person_normal);
    }

    @Override
    public void setValue(String str) {
        Log.d("=====str",str);
        tv_three.setText(str);
    }
}
